package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;


    /*@Override
        public PageResult<Emp> page(Integer page, Integer pageSize) {
        Long totaol = empMapper.count();
        Integer start = (page-1)*pageSize;
        List<Emp> list = empMapper.list(start,pageSize);
        PageResult<Emp> pageResult = new PageResult<>(totaol,list);
        return pageResult;
    }
    */
    //基于pageHelper进行分页查询
    @Override
    public PageResult<Emp> page(EmpQueryParam  param) {
        //设置分页参数
        PageHelper.startPage(param.getPage(),param.getPageSize());
        //执行查询
        List<Emp> empList = empMapper.list(param);
        //解析查询结果并封装
        Page<Emp> p = (Page<Emp>)empList;
        PageResult<Emp> pageResult = new PageResult<>(p.getTotal(),empList);
        return pageResult;
    }
    //事务管理
    //在业务方法运行之前开启事务,在业务方法运行之后判断业务方法中是否出现运行时异常，如果出现运行时异常则回滚事务，如果正常结束则提交事务
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(Integer []ids) {
        empMapper.delete(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public void add(Emp emp) {
        //前端未请求的参数，在Service层进行赋值
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
        List<EmpExpr> exprList = emp.getExprList();
        //一个人可以有多段工作经验,遍历experList为每段工作经验设置empId表示属于同一个人
        if(!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            //执行sql语句操作数据库
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public Emp getInfo(Integer id) {
       return empMapper.getInfo(id);
    }

    @Override
    public void update(Emp emp) {
         empMapper.update(emp);
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e = empMapper.selectByUsernameAndPassword(emp);
        if(e != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            log.info("用户{}登录了系统,令牌为{}",e.getName(),jwt);
            return new LoginInfo(e.getId(), e.getName(), e.getUsername(), jwt);
        }else {
            return null;
        }
    }

    @Override
    public List<Emp> list() {
        List<Emp> empList = empMapper.findAll();
        return empList;
    }
}
